package com.interceptlonggraph;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by worm on 2017/6/27.
 */

public class InterceptLongGraphModule extends ReactContextBaseJavaModule {

    public ReactContext context;

    public ViewGroup ViewGroup;

    public  Bitmap bitmap;

    public InterceptLongGraphModule(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
    }

    @Override
    public String getName() {
        return "InterceptLongGraphModule";
    }



    public Bitmap Intercept(int  id){

        ergodicView((ViewGroup) context.getCurrentActivity().findViewById(android.R.id.content),id);

        bitmap=compressImage(getBitmapByView(ViewGroup));




        return bitmap;

    }

    /**
     * 压缩图片
     *
     * @param bitmapByView
     * @return
     */
    private Bitmap compressImage(Bitmap bitmapByView) {

        ByteArrayOutputStream baos=new ByteArrayOutputStream();

        // 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        bitmapByView.compress(Bitmap.CompressFormat.JPEG,100,baos);

        int options =100;
        // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
           while (baos.toByteArray().length / 1024 > 100){
               // 重置baos
               baos.reset();
               // 这里压缩options%，把压缩后的数据存放到baos中
               bitmapByView.compress(Bitmap.CompressFormat.JPEG, options, baos);
               // 每次都减少10
               options -= 10;
           }
        // 把压缩后的数据baos存放到ByteArrayInputStream中
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        // 把ByteArrayInputStream数据生成图片
        Bitmap bitmap = BitmapFactory.decodeStream(bais, null, null);

        return  bitmap;

    }

    /**
     * 截取ViewGroup的屏幕
     *
     * @param viewGroup
     * @return
     */
    private Bitmap getBitmapByView(ViewGroup viewGroup) {

//        int h=0;
        Bitmap mBitmap;

//        for(int i=0;i<viewGroup.getChildCount();i++){
//            h+=viewGroup.getChildAt(i).getHeight();
//
//        }
        mBitmap=Bitmap.createBitmap(viewGroup.getWidth(),viewGroup.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas=new Canvas(mBitmap);
        viewGroup.draw(canvas);
        return  mBitmap;

    }

    private void ergodicView(ViewGroup viewGroup ,int id) {

        for(int i=0;i< viewGroup.getChildCount();i++){
            View view = viewGroup.getChildAt(i);
            if(view instanceof ViewGroup){

                if (id == view.getId()) {
//                    System.out.println("test"+"------------>"+view.getId());
                    ViewGroup = (ViewGroup) view;
                }
                ergodicView((ViewGroup) viewGroup.getChildAt(i),id);
            }

        }

    }



}
